﻿@model Xms.Web.Models.RoleModel

<div class="panel panel-default" id="gridview">
    <div class="panel-heading">
        <div class="panel-title">
            <strong>@app.PageTitle</strong>
            <div class="pull-right">
                <a class="btn btn-info btn-xs" data-toggle="collapse"
                   href="#collapseOne">
                    <span class="caret"></span> @app.T["search"]
                </a>
                <a class="btn btn-info btn-xs" href="@("/"+app.OrganizationUniqueName)/role/editrole"><span class="glyphicon glyphicon-file"></span> @app.T["add"]</a>
            </div>
        </div>
    </div>
    <div id="collapseOne" class="panel-collapse collapse">
        <div class="panel-body">
            @using (Html.BeginForm("index", "role", FormMethod.Get, new { @id = "searchForm", @class = "form-horizontal", @role = "form" }))
            {
                @Html.ValidationSummary()
                <div class="form-group col-sm-6">
                    <label class="col-sm-3 control-label" for="clientip">@app.T["name"]</label>
                    <div class="col-sm-9">
                        @Html.TextBoxFor(x => x.Name, new { @class = "form-control xmsform-item", data_type = "nvachart" })
                    </div>
                </div>
                <div class="form-group col-sm-6">
                    <label class="col-sm-3 control-label" for="url">@app.T["statecode"]</label>
                    <div class="col-sm-9">
                        <label class="checkbox-inline">
                            @Html.RadioButtonFor(x => x.State, 1, new { @class = "xmsform-item", data_type = "state" }) @app.T["enabled"]
                        </label>
                        <label class="checkbox-inline">
                            @Html.RadioButtonFor(x => x.State, 0, new { @class = "xmsform-item", data_type = "state" }) @app.T["disabled"]
                        </label>
                    </div>
                </div>
                <div class="form-group col-sm-12 text-center">
                    <button type="submit" class="btn btn-primary btn-sm btn-block"><span class="glyphicon glyphicon-search"></span> @app.T["search"]</button>
                </div>
            }
        </div>
    </div>
    <div style="padding:15px;">
        <div class="datagrid-view">
        </div>
    </div>
    <div class="table-responsive" style="overflow:visible;">

        <table class="table table-striped table-hover table-condensed" id="datatable">

            <tfoot>
                <tr>
                    <td colspan="6" class="active">
                        <button type="button" class="btn btn-xs btn-primary" data-action="/role/deleterole" data-role="delete"><span class="glyphicon glyphicon-trash"></span> 批量删除</button>
                        <div class="btn-group dropup">
                            <button type="button" class="btn btn-xs btn-primary dropdown-toggle"
                                    data-toggle="dropdown">
                                <span class="glyphicon glyphicon-edit"></span> @app.T["batch_update"]
                                <span class="caret"></span>
                            </button>
                            <ul class="dropdown-menu">
                                <li><a href="#" data-action="/api/role/SetRoleState" data-role="update" data-actiondata='{"isenabled":true}'>@app.T["enabled"]</a></li>
                                <li><a href="#" data-action="/api/role/SetRoleState" data-role="update" data-actiondata='{"isenabled":false}'>@app.T["disabled"]</a></li>
                            </ul>
                        </div>
                    </td>
                </tr>
            </tfoot>
        </table>
    </div>

    @*<div class="panel-footer">
            <div class="row">
                <div class="col-sm-4">
                    @(app.T["pagination_label"].Replace("{page}", Model.Page.ToString()).Replace("{totalpages}", Model.TotalPages.ToString()).Replace("{totalitems}", Model.TotalItems.ToString()))
                </div>
                <div id="page-selection" class="col-sm-8" data-total="@Model.TotalPages" data-page="@Model.Page"></div>
            </div>
        </div>*@
</div>
<!-- （Modal） -->
<div class="modal fade" id="copyModal" tabindex="-1" role="dialog"
     aria-labelledby="copyModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal"
                        aria-hidden="true">
                    @app.T["times_sign"]
                </button>
                <h4 class="modal-title" id="navGroupModalLabel">
                    @(app.T["copy"] + app.T["role"])
                </h4>
            </div>
            <div class="modal-body">
                <form id="copyForm" action="@("/"+app.OrganizationUniqueName)/role/CopyRole" method="post" class="form-horizontal">
                    <input name="roleid" type="hidden" />
                    <div class="form-group row">
                        <label for="" class="col-sm-2 col-md-2 col-xs-2  control-label">@app.T["name"]</label>
                        <div class="col-sm-10 col-md-10 col-xs-10">
                            <input id="name" name="name" type="text" class="form-control input-sm" />
                        </div>
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default"
                        data-dismiss="modal">
                    <span class="glyphicon glyphicon-remove"></span> @app.T["cancel"]
                </button>
                <button type="button" class="btn btn-primary" onclick="CopyPost()">
                    <span class="glyphicon glyphicon-ok"></span> @app.T["confirm"]
                </button>
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal-dialog -->
</div><!-- /.modal -->

@section Header {
    <link href="/content/js/bootstrap-datepicker-1.5.0/css/bootstrap-datepicker3.min.css?v=@app.PlatformSettings.VersionNumber" rel="stylesheet">
    <link href="~/content/js/jquery-ui-1.10.3/themes/base/jquery.ui.all.css?v=@app.PlatformSettings.VersionNumber" rel="stylesheet">
    <link href="~/content/js/grid/pqgrid.dev.css?v=@app.PlatformSettings.VersionNumber" rel="stylesheet">
}
@section Scripts {
    <script type="text/html" id="rolelistTmpl">
        @*@foreach (var resourceOwner in Xms.Security.Abstractions.ResourceOwnerCollection.Descriptors)
            {
                if (resourceOwner.Value.UIEndpoint.IsNotEmpty())
                {
                    <li><a href="/@app.OrganizationUniqueName@resourceOwner.Value.UIEndpoint?roleid={{roleid}}"><span class="glyphicon glyphicon-lock"></span> @app.T[resourceOwner.Key].IfEmpty(resourceOwner.Key)</a></li>
                }
                else
                {
                <li><a href="EditRolePermissions?roleid={{roleid}}&resourcename=@resourceOwner.Key"><span class="glyphicon glyphicon-lock"></span> @app.T[resourceOwner.Key].IfEmpty(resourceOwner.Key)</a></li>
                }
            }*@
    </script>
    <script>

        var theaders = {
            name:' @app.T["name"]',
            statecode:'@app.T["statecode"]',
            create_time:'@app.T["create_time"]',
             displayname:'权限',
            operation:'@app.T["operation"]'
        }
        var tags = {
            enabled: '@app.T["enabled"]',
            disabled:'@app.T["disabled"]'
        }
        var adminName = '@Xms.Security.Abstractions.RoleDefaults.ADMINISTRATOR';
    </script>
    <script src="/content/js/jquery-ui-1.10.3/ui/jquery.ui.button.js?v=@app.PlatformSettings.VersionNumber"></script>
    <script src="/content/js/jquery-ui-1.10.3/ui/jquery.ui.mouse.js?v=@app.PlatformSettings.VersionNumber"></script>
    <script src="/content/js/jquery-ui-1.10.3/ui/jquery.ui.autocomplete.js?v=@app.PlatformSettings.VersionNumber"></script>
    <script src="/content/js/jquery-ui-1.10.3/ui/jquery.ui.draggable.js?v=@app.PlatformSettings.VersionNumber"></script>
    <script src="/content/js/jquery-ui-1.10.3/ui/jquery.ui.resizable.js?v=@app.PlatformSettings.VersionNumber"></script>
    <script src="/content/js/jquery-ui-1.10.3/ui/jquery.ui.tooltip.js?v=@app.PlatformSettings.VersionNumber"></script>
    <script src="~/content/js/fetch.js?v=@app.PlatformSettings.VersionNumber"></script>
    <script src="~/content/js/common/filters.js?v=@app.PlatformSettings.VersionNumber"></script>
    <script src="/content/js/grid/pqgrid.dev.js?v=@app.PlatformSettings.VersionNumber"></script>
    <script src="/content/js/grid/localize/pq-localize-zh.js?v=@app.PlatformSettings.VersionNumber"></script>
    <script src="/content/js/cdatagrid.js?v=@app.PlatformSettings.VersionNumber"></script>
    <script src="/content/js/jquery.bootpag.min.js?v=@app.PlatformSettings.VersionNumber"></script>
    <script src="/content/js/jquery.form.js?v=@app.PlatformSettings.VersionNumber"></script>
    <script src="/content/js/bootstrap-datepicker-1.5.0/js/bootstrap-datepicker.min.js?v=@app.PlatformSettings.VersionNumber"></script>
    <script src="/content/js/bootstrap-datepicker-1.5.0/locales/bootstrap-datepicker.zh-CN.min.js?v=@app.PlatformSettings.VersionNumber" charset="UTF-8"></script>
    <script>
        var pageUrl = $("#datatable").attr('data-pageurl');

        $(function () {
            $('.datepicker').datepicker({
                autoclose: true
                , clearBtn: true
                , format: "yyyy-mm-dd"
                , language: "zh-CN"
            });

            //$('#searchForm').ajaxSearch('#gridview', ajaxgrid_reset);
            //$("#datatable").ajaxTable();
            //ajaxgrid_reset();

            var rolehtml = $('#rolelistTmpl').html();
            //列数据配置数据
            var columnConfigs = [
                //从新配置复选框列的渲染方式，
                {
                    title: "", dataIndx: "recordid", maxWidth: 48, minWidth: 48, align: "center", resizable: false,
                    type: 'checkBoxSelection', cls: 'ui-state-default', sortable: false, editable: false,
                    render: function (ui) {
                          console.log(ui)
                        return '<input type="checkbox" value="' + ui.rowData.roleid + '" name="recordid" class="">'
                    },
                    cb: { all: true, header: true }
                },
                {
                    "dataIndx": "name", "title": theaders.name, "dataType": "string", editable: false, "width": 100, "isprimaryfield": false, "attributetypename": "string", render:function(ui) {
                        var datas = ui.rowData;
                        var dataIndx = ui.dataIndx;
                        var column = ui.column;
                        var record = datas[dataIndx];
                        var name = datas['name'];
                        var roleid = datas['roleid'];
                        return '<a class="text-primary" href="' + ORG_SERVERURL + '/role/editrole?id=' + roleid + '">' + name+'</a>';
                    }
                },
                {
                    "dataIndx": "state", "title": theaders.statecode, editable: false, align: 'center', "dataType": "string", "width": 100, "isprimaryfield": false, "attributetypename": "string"
                    ,render: function(ui){
                        var datas = ui.rowData;
                        var dataIndx = ui.dataIndx;
                        var column = ui.column;
                        var record = datas[dataIndx];
                        if (record == "1") {
                            return '<span class=\"label label-info\">' + tags.enabled + '</span>'
                        } else {
                            return '<span class=\"label label-primary\">' + tags.disabled + '</span>'
                        }
                    }
                },

                { "dataIndx": "createdon", "title": theaders.create_time, editable: false, "dataType": "string", "width": 100, "isprimaryfield": false, "attributetypename": "string" },
                {
                    "dataIndx": "displayname", "title": theaders.displayname, editable: false, "dataType": "string", "width": 100,align:'center', "isprimaryfield": false, "attributetypename": "string", sortable: false,
                    render: function (ui) {
                        var datas = ui.rowData;
                        var dataIndx = ui.dataIndx;
                        var column = ui.column;
                        var name = datas['name'];
                        var roleid = datas['roleid'];
                        if (name != adminName) {
                            var rowhtml =  '<div class="btn-group btn-group-xs datatable-filter-wrap">'+
                                   ' <button type="button" class="btn btn-info dropdown-toggle" data-toggle="dropdown">'+
                                       ' <span class="glyphicon glyphicon-lock"></span> 授权'+
                                       ' <span class="caret"></span>'+
                                   ' </button>'+
                                   ' <ul class="dropdown-menu">'

                            var itemroleHtml = rolehtml;
                            itemroleHtml = itemroleHtml.replace(/{{roleid}}/g, roleid);
                            return rowhtml + itemroleHtml+' </ul></div>';
                        } else {
                            return '';
                        }
                    }
                },
                {
                    title: "操作", editable: false, minWidth: 125, notHeaderFilter: true, editable: false, sortable: false, render: function (ui) {
                        var datas = ui.rowData;
                        var dataIndx = ui.dataIndx;
                        var column = ui.column;
                        var name = datas['name'];
                        var roleid = datas['roleid'];
                        if (name != adminName) {
                            return '<a class="btn btn-default btn-xs " href="' + ORG_SERVERURL+'/role/editrole?id=' + roleid + '"><span class="glyphicon glyphicon-edit"></span> 编辑</a> ' +
                                '<a class="btn btn-warning btn-xs text-white" href="javascript:Xms.Web.Del(\'' + roleid+'\',\'' + ORG_SERVERURL+'/role/deleterole\')"><span class="glyphicon glyphicon-trash"></span> 删除</a> ' +
                                '<a class="btn btn-default btn-xs" href="javascript:CopyRole(\''+roleid+'\')"><span class="glyphicon glyphicon-file"></span> 复制</a> ';
                        } else {
                            return '';
                        }
                    }
                }
            ];

            var $form = $('#searchForm');
            var roles_filters = new XmsFilter();
            var datagridconfig = {
                baseUrl: ORG_SERVERURL + '/api/role/getall',
                columnConfigs: columnConfigs,//字段配置信息
                context: $('#gridview'),//底部操作按钮方法触发
                filters: roles_filters,//post提交时过滤条件
                searchForm: $('#searchForm')//GET提交时查询的数据
            };
            //  console.log(itemtmpl);
            $('.datagrid-view').xmsDataTable(datagridconfig);

        });
        //function ajaxgrid_reset() {
        //    pag_init();
        //    Xms.Web.DataTable($("#datatable"));
        //}
        //function pag_init() {
        //    $('#page-selection').bootpag({
        //        total: $('#page-selection').attr('data-total')
        //        , maxVisible: 10
        //        , page: $('#page-selection').attr('data-page')
        //        , leaps: false
        //        , prev: '&lsaquo;'
        //        , next: '&rsaquo;'
        //        , firstLastUse: true
        //        , first: '&laquo;'
        //        , last: '&raquo;'
        //    }).on("page", function (event, /* page number here */ num) {
        //        event.preventDefault();
        //        var url = $.setUrlParam(pageUrl, 'page', num);
        //        $("#gridview").ajaxLoad(url, "#gridview", function (response) {
        //            //$("#gridview").replaceWith($(this)); // some ajax content loading...
        //            ajaxgrid_reset();
        //        });
        //        return false;
        //    });
        //}

        //function rebind() {
        //    $('#searchForm').submit();
        //}

        function CopyRole(id) {
            $('#copyForm').find('input[name=roleid]').val(id);
            $('#copyModal').modal('show');
        }
        function CopyPost() {
            $('#copyForm').ajaxForm(function (response) {
                if (response.IsSuccess) {
                    Xms.Web.Alert(true, response.Content);
                    location.reload(true);
                    return;
                }
                Xms.Web.Alert(false, response.Content);
            });
            $('#copyForm').submit();
        }
    </script>
}